package com.xin.data.simbapesa.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewbinding.ViewBinding;

import com.bumptech.glide.Glide;
import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.base.BaseBindingAdapter1;
import com.xin.data.simbapesa.databinding.SimbaAccountItem1Binding;
import com.xin.data.simbapesa.databinding.SimbaAccountItem2Binding;
import com.xin.data.simbapesa.obj.SimbaAccountItem;
import com.xin.data.simbapesa.utils.ResUtils;
import com.xin.data.simbapesa.utils.ViewUtils;

import java.util.List;

public class AccountAdapter extends BaseBindingAdapter1<SimbaAccountItem, ViewBinding> {


    private boolean isEdit;

    private boolean isSummit;

    public AccountAdapter(List<SimbaAccountItem> dataList) {
        super(dataList);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void bindData(ViewBinding binding, SimbaAccountItem item, int position) {
        if (binding instanceof SimbaAccountItem1Binding) {
            SimbaAccountItem1Binding binding1 = (SimbaAccountItem1Binding) binding;
            if (item.isShowName()) {
                binding1.tvMustLayout.setVisibility(View.VISIBLE);
                binding1.tvName.setText(ResUtils.s(R.string.account_item_title) + " " + (position + 1));
            } else {
                binding1.tvMustLayout.setVisibility(View.GONE);
            }
            binding1.editLayout.setOnClickListener(null);
            binding1.editLayout.setOnClickListener(v -> {
                ViewUtils.viewScale(v);
                if (onBtnItemClickListener != null) {
                    onBtnItemClickListener.onItemClick(position);
                }
            });
        } else if (binding instanceof SimbaAccountItem2Binding) {
            SimbaAccountItem2Binding binding1 = (SimbaAccountItem2Binding) binding;
            if (item.isShowName()) {
                binding1.tvMustLayout.setVisibility(View.VISIBLE);
                binding1.tvName.setText(ResUtils.s(R.string.account_item_title) + " " + (position + 1));
            } else {
                binding1.tvMustLayout.setVisibility(View.GONE);
            }
            Context context = binding1.tvIcon.getContext();
            Glide.with(context).load(item.getWalletLogo()).placeholder(R.color.placeholder_icon).into(binding1.tvIcon);
            binding1.tvTitle.setText(item.getWalletDesc());
            binding1.tvSubTitle.setText(item.getWalletMobile());
            if (position > 0) {
                binding1.tvMust.setVisibility(View.GONE);
            } else {
                binding1.tvMust.setVisibility(View.VISIBLE);
            }

            if (isEdit) {
                binding1.tvDelete.setVisibility(View.VISIBLE);
                binding1.tvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ViewUtils.viewScale(v);
                        PU.userBuriedPoint(PU.P53, "");
                        if (onBtnItemClickListener != null) {
                            onBtnItemClickListener.onItemClick(position);
                        }
                    }
                });
            } else {
                binding1.tvDelete.setVisibility(View.GONE);
            }


            if (isSummit) {
                binding1.editLayout.setBackground(ResUtils.de(R.drawable.account_bg2));
            } else {
                binding1.editLayout.setBackground(ResUtils.de(R.drawable.account_item_bg2));
            }


        }
    }

    @Override
    protected int getItemType(SimbaAccountItem item) {
        if (item.isData()) {
            return 1;
        }
        return 0;
    }

    @Override
    protected ViewBinding createBinding(LayoutInflater inflater, ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return SimbaAccountItem2Binding.inflate(inflater, parent, false);
        }
        return SimbaAccountItem1Binding.inflate(inflater, parent, false);
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }


    public boolean isSummit() {
        return isSummit;
    }

    public void setSummit(boolean summit) {
        isSummit = summit;
    }
}
